package com.pinusmusic.dao.impl;

import com.pinusmusic.dao.Music_MusicListDao;
import com.pinusmusic.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;


public class Music_MusicListDaoImpl implements Music_MusicListDao {
    private  JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public int getMusicCountByMlid(int mlid) {
        String sql = "select count(*) from music_musiclist where mlid=?";
        return template.queryForObject(sql,Integer.class,mlid);
    }

    @Override
    public void deleteMusicByMlid(int mlid) {
        String sql = "delete from music_musiclist where mlid=?";
        template.update(sql,mlid);
    }

    @Override
    public void deleteMusicByMid(int mid) {
        String sql = "delete from music_musiclist where mid=?";
        template.update(sql,mid);
    }

    @Override
    public void addMusic(int mlid, int mid) {
        String sql = "insert into music_musiclist(mlid,mid) values(?,?)";
        template.update(sql,mlid,mid);
    }

    @Override
    public List<Integer> findMusicByMlid(int mlid) {
        String sql = "select mid from music_musiclist where mlid=?";
        List<Integer> milList = template.queryForList(sql, Integer.class, mlid);
        return milList.size()==0?null:milList;
    }

    @Override
    public void remMusic(Integer mlid, int mid) {
        String sql = "delete from music_musiclist where mlid=? and mid=?";
        template.update(sql,mlid,mid);
    }

    @Override
    public boolean findMusicByMlidAndMid(int mlid, int mid) {
        String sql = "select count(*) from music_musiclist  where mlid=? and mid=?";
        Integer count = template.queryForObject(sql, Integer.class, mlid, mid);
        return count==0;
    }
}
